import { hasToken } from '@/utils/token'
import React, { ComponentType } from 'react'
import { Redirect, Route, RouteProps } from 'react-router-dom'

// ...rest获取剩余参数
// 重命名, 因为下面组件名默认首字母要大写
// RouteProps Route组件的props类型
// ComponentType react内置的组件类型

export default function AuthRoute({ component, ...rest }: RouteProps) {
  // rest = {path:'/xxx', exact: true, a: '', b:''}
  const Component = component as ComponentType

  return (
    <Route
      //   path={path}
      //   exact={exact}
      {...rest}
      render={(props) => {
        // 路由信息对象 { history: xxx, location: xxx, match: xxxx, ....}
        console.log('1212', props)

        if (!hasToken())
          return (
            <Redirect
              to={{
                pathname: '/login',
                state: {
                  from: props.location.pathname, // 标记将来登录成功以后需要会跳到哪个页面
                },
              }}
            />
          )
        return <Component />
      }}
    ></Route>
  )
}
